package com.wson.widget.render.compass.framework;

import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.wson.behavior.ctrl.IBehavior;
import com.wson.behavior.element.IClickable;
import com.wson.behavior.element.ITableElement;
import com.wson.behavior.layout.ITable;
import com.wson.behavior.property.ITableRow;
import com.wson.constant.compass.IRuntimeConstant;
import com.wson.function.compass.ActiveStatusFunc;
import com.wson.function.wolfow.NextRoutesFunc;
import com.wson.model.basic.IBasicView;
import com.wson.model.compass.status.ICompassStatusMeta;
import com.wson.model.compass.status.ICompassStatusView;
import com.wson.model.compass.status.impl.CompassStatusModel;
import com.wson.model.framework.user.impl.UserModel;
import com.wson.model.wolfow.flow.impl.WolfFlowModel;
import com.wson.model.wolfow.route.IWolfRouteView;
import com.wson.model.wolfow.step.IWolfStepView;
import com.wson.model.wolfow.step.impl.WolfStepModel;
import com.wson.platform.factory.impl.BeanFactory;
import com.wson.root.util.LoggerUtil;
import com.wson.util.function.JScriptUtil;
import com.wson.widget.render.IRenderParam;
import com.wson.widget.render.function.sterotype.AbstractFeedsTableRender;
import com.wson.widget.render.layout.sterotype.AbstractModelFormRender;
import com.wson.widget.translator.ITableTranslator;
import com.wson.widget.translator.impl.TableTranslatorImpl;

public class FeedsRender extends AbstractFeedsTableRender {

	public final static String PARAM_CSSAVAILABLE = "CSSAVAILABLE";	
	
	@IRenderParam(name=PARAM_CSSAVAILABLE)
	private Boolean cssAvailable = false;
	
	@Override
	public String code() {
		return "FEEDSRENDER";
	}
	
	@Override
	protected IClickable generateModifyAction() {
		return null;
	}

	@Override
	protected IClickable generateRemoveAction() {
		return null;
	}




	public Boolean getCssAvailable() {
		return cssAvailable;
	}

	public void setCssAvailable(Boolean cssAvailable) {
		this.cssAvailable = cssAvailable;
	}

	@Override
	public Long getModelMetaID() {
		return ICompassStatusMeta.META_ID;
	}

	@Override
	public IBehavior render() {
		
				
		try{
			Long userid = this.getSessionProfile().getUser().getID();
			//System.out.println("userid "+userid);
			ActiveStatusFunc func=new ActiveStatusFunc();
			this.initFunc(func);
			List<ICompassStatusView> statuses = func.function(userid);
			//System.out.println("statuses "+statuses.size());
			if(statuses!=null 
					&& statuses.size()>0){
				for(ICompassStatusView status : statuses){
					this.addModel(status);
				}
			}
			
		}
		catch(Exception ex){
			//System.out.println(ex.getMessage());
			//ex.printStackTrace();
			LoggerUtil.DEBUG(ex);
		}

		
		ITable table = (ITable) super.render();
		table.getAppearance().setWidth("100%");
		table.setEmptyText(this.getMessage(IRuntimeConstant.SYNC_NOFEEDS));
		//System.out.println("******** body getContainerID "+getContainerID());
		return table;
	}

	
	
	@Override
	protected IClickable generateCreateAction() {
		IClickable action = super.generateCreateAction();
		//System.out.println("action "+action.generateBehaviorCode());
		return action;
	}

	@Override
	protected AbstractModelFormRender generateCreateRender() {
		StatusFlowRender render = new StatusFlowRender();
		return render;
	}

	public  IBasicView  generateTask(Long id, String shorttext, String longtext, String icon, String image, IBasicView child){
		CompassStatusModel test=new CompassStatusModel();
		this.initModel((IBasicView)test);
		
		test.setID(id);
		test.setShortText(shorttext);
		test.setLongText(longtext);
		test.setIconLink(icon);
		test.setImageLink(image);
		test.setGraphicLink(image);
		test.setCreateDate(new Date());
		
		//if(child!=null)
		test.addChild(child);
		
		return test;
	}
	@Override
	public ITableTranslator getTranslator() {
		return new TableTranslatorImpl(){
			@Override
			protected  ITableRow  translateRow(IBasicView model, int behaviortype, int index){
				
				if(model==null)
					return null;
				
				ICompassStatusView status = (ICompassStatusView)model;
				
				ITableRow tablerow = super.translateRow(model, behaviortype, index);
				
				String author = "";
				if(status.getCreateUserID().equals(this.getSessionProfile().getUser().getID()))
					author = this.getMessage(IRuntimeConstant.SYNC_MYSENT);//"Sent by me";
				else{
					try{
						UserModel user = new UserModel();
						this.initModel((IBasicView)user);
						user.setID(status.getCreateUserID());
						//System.out.print("status.getCreateUserID() "+status.getCreateUserID());
						if(user.selfSelect())
							author = this.getMessage(IRuntimeConstant.SYNC_RECEIVEDFROM)+user.getName();						
					}
					catch(Exception ex){
						LoggerUtil.DEBUG(ex);
					}
				}
				tablerow.addElement(generateAction(author, null));
				tablerow.addElement(generateAction(""+status.getCreateDate(), null));
				try{
					if(status.getCreateUserID().equals(this.getSessionProfile().getUser().getID())){
						tablerow.addElement(
									generateCloseAction(status)
								);	
					}
				}
				catch(Exception ex){
					LoggerUtil.DEBUG(ex);
				}
				//System.out.println("status.getInboundStepID() "+status.getInboundStepID());
				if(status.getInboundStepID()!=null){
					try{
						WolfStepModel step = new WolfStepModel();
						if(!status.getCreateUserID().equals(
								this.getSessionProfile().getUser().getID())){
							tablerow.addElement(
									generateForwardAction(status)
								);	
						}
						this.initModel((IBasicView)step);
						step.setID(status.getInboundStepID());
						if(step.selfSelect()){
							//System.out.println("step.getFlowID() "+step.getFlowID());
							if(step.getFlowID()!=null){
								WolfFlowModel flow = new WolfFlowModel();
								this.initModel((IBasicView)flow);
								flow.setID(step.getFlowID());
								if(flow.selfSelect()
										&& !Boolean.TRUE.equals(flow.getClosed())){
									
									//System.out.println("step.getRouteID() "+step.getRouteID());
									NextRoutesFunc nfunc=new NextRoutesFunc();
									this.initFunc(nfunc);
									Collection<IWolfRouteView> routes = nfunc.function(step.getRouteID());
									if(routes!=null && routes.size()>0){
										
										for(IWolfRouteView route:routes)
											tablerow.addElement(generateWorkflowAction(status, step, route));
									

									}
									
									
								}
							}
							

						}
					}
					catch(Exception ex){
						LoggerUtil.DEBUG(ex);
					}
				}
							
				
				return tablerow;
			}
			protected  ITableElement generateForwardAction(ICompassStatusView status)throws Exception{
				//table = this.result;
				//table.setBehaviorID(getBehaviorID());
				
				
				StatusFlowRender render = new StatusFlowRender();
				initRenderModel(render);
				render.setBehaviorType((int)ICompassStatusMeta.BEHAVIORSTRUCTURE_STATUSFORWARD);
				render.setRetweetedStatusID(status.getID());
				render.setCssAvailable(getCssAvailable());
				//render.(status.getFlowID());
				
				
				render.setFramed(false);
				CompassStatusModel retweet = new CompassStatusModel();
				status.initModel((IBasicView)retweet);
				
				render.setModel(retweet);
				//table not existed yet.
				IClickable action = generateLayoutPopupAction(
						render 
						, this.getMessage(IRuntimeConstant.SYNC_FORWARD)
						, false);
				action.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
				action.getNavigation().setHref(JScriptUtil.generateScriptVoid());
				
				
				
				return action;
			}
			protected  ITableElement generateCloseAction(ICompassStatusView status)throws Exception{
				
				WolfFlowModel flow = new WolfFlowModel();
				this.initModel((IBasicView)flow);
				flow.setID(status.getFlowID());
				//System.out.println("flow.getID() "+flow.getID());
				if(flow.getID()!=null 
						&& flow.selfSelect()
						){
					//System.out.println("flow.getClosed() "+flow.getClosed());
					if(Boolean.TRUE.equals(flow.getClosed())){
						IClickable label = BeanFactory.getCurrentFactory().getBehaviorFactory().getClickable();
						this.initBehavior(label);
						label.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
						label.getDepict().setTitle(this.getMessage(IRuntimeConstant.SYNC_CLOSED));
						label.getNavigation().setHref(JScriptUtil.generateScriptVoid());
						label.getAppearance().setDisabled(true);
						return label;
					}
					else{
						//table = this.result;
						//table.setBehaviorID(getBehaviorID());
						
						ConfirmFlowCloseRender render = initRender(new ConfirmFlowCloseRender());
						render.setModelID(status.getFlowID());
						render.setConfirmationText("Do you want to close the flow?");
						IClickable action = generateLayoutPopupAction(
								render,	this.getMessage(IRuntimeConstant.SYNC_CLOSE), false);
						action.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
						action.getNavigation().setHref(JScriptUtil.generateScriptVoid());
						
						return action;
					}
				}
				
				return null;
			}
			
			
			protected  IClickable generateWorkflowAction(ICompassStatusView status, IWolfStepView step, IWolfRouteView route)throws Exception{

				//System.out.println(" workflow table behavior "+getBehaviorID()+" container "+getContainerID());
				
				StatusFlowRender render = new StatusFlowRender();
				initRenderModel(render);
				render.setBehaviorType((int)ICompassStatusMeta.BEHAVIORSTRUCTURE_STATUSREPLY);
				render.setRetweetedStatusID(status.getID());
				render.setReplyUserID(status.getCreateUserID());	
				render.setCssAvailable(getCssAvailable());

				render.setFlowID(status.getFlowID());
				render.setInboundEventID(status.getOutboundEventID());
				render.setInboundRouteID(route.getID());
				
				
				render.setFramed(false);
				CompassStatusModel retweet = new CompassStatusModel();
				status.initModel((IBasicView)retweet);
				
				render.setModel(retweet);
				//table not existed yet.
				IClickable action = generateLayoutPopupAction(
						render 
						, route.getShortText()
						, false);
				action.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
				action.getNavigation().setHref(JScriptUtil.generateScriptVoid());
				
				//System.out.println("workflow action "+action.generateBehaviorCode());
				
				return action;
			}
			protected  IClickable generateAction(String text, String href){
				IClickable action = BeanFactory.getCurrentFactory().getBehaviorFactory().getClickable();
				this.initBehavior(action);
				action.getAppearance().setType(IClickable.APPEARANCETYPE_LINK);
				action.getAppearance().setDisabled(false);
				action.getDepict().setTitle(text);
				action.getNavigation().setHref(href);
				return action;
			}
		};
	}
	
	
}
